﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FutureOpen.Web.Pages;
using FutureOpen.Core;
using FutureOpen.Core.func;
using FutureOpen.Core.func.db;
using FutureOpen.Core.func.sql;
using System.Text;
public partial class controls_OrgView : UserControl
{

    private int orgid;
    private int uid;
    private PageBase pbase;
    /*
    MyOrg ObjMyOrg = new MyOrg();
    MyUsr ObjMyUsr = new MyUsr();
    MyOrgusr ObjMyOrgusr = new MyOrgusr();
    */ 
    protected void Page_Load(object sender, EventArgs e)
    {
        
        pbase = (PageBase)this.Page;
        pbase.PageInit();

        if (pbase.orgid > 0)
        {
            this.orgid = pbase.orgid;           
        }
        if (pbase.IsLogin())
        {
            uid = pbase.MyUsr;
        }       
        
        if (!this.IsPostBack && orgid> 0)
        {
            DataRow dr = GroupProvider.GetGroupByID(orgid);
            
            this.ImageOrg.ImageUrl = "../test/GetImage.ashx?t=1&id=" + dr["group_pic"].ToString();
            this.lblName.Text = dr["group_name"].ToString();
            this.lblDesc.Text = ShortenContent(dr["group_desc"].ToString());

            this.lblHot.Text = dr["group_person_count"].ToString();
            this.lblArtCount.Text = dr["group_art_count"].ToString();


            DataSet ds = GroupProvider.GetGroupAdminUsrByGroup(orgid);
            StringBuilder sb = new StringBuilder();
            
            foreach (DataRow datarow in ds.Tables[0].Rows)
            {
                sb.Append(pbase.GetUsrUrl(datarow) + ";&nbsp;&nbsp;");
            }
            sb.Remove(sb.Length - 13, 13);
            this.Literal1.Text = sb.ToString();


            DataRow drtype = GroupProvider.GetGroupUsrByGroup(uid, orgid);

            if (drtype == null) this.linkBt_AplOrQt.Text = "申请加入";
            else
            {
                if (Int16.Parse(drtype["groupusr_type"].ToString()) ==  GroupProvider.APPLY)
                {
                    this.linkBt_AplOrQt.Text = "申请加入中...";
                    this.linkBt_AplOrQt.Visible = false;
                    this.LblApplying.Text = "申请加入中...";
                    this.LblApplying.Visible = true;
                }
                else if(Int16.Parse(drtype["groupusr_type"].ToString()) ==  GroupProvider.ADMIN)
                {
                   this.LinkBt_EditOrg.Visible = true;
                }
                this.linkBt_AplOrQt.Text = "退出组织";
                StringBuilder strbld = new StringBuilder("javascript: return confirm('");
                strbld.Append("确实要退出当前组织么？");
                strbld.Append("')");
                this.linkBt_AplOrQt.Attributes["onClick"] = strbld.ToString();   
            }
        }

    }
    public string ShortenContent(string content)
    {
        if (content.Length > 100)
        {
            return content.Substring(0, 97) + "...";
        }
        return content;
    }
    protected void linkBt_NewOrg_Click(object sender, EventArgs e)
    {
        Response.Redirect("GroupAdd.aspx");
    }
    protected void linkBt_AplOrQt_Click(object sender, EventArgs e)
    {
        if (this.linkBt_AplOrQt.Text == "申请加入")
        {
            try
            {

                GroupProvider.ApplyGroup(orgid, uid, GroupProvider.APPLY);

                {
                    this.linkBt_AplOrQt.Text = "申请加入中...";
                    this.linkBt_AplOrQt.Visible = false;
                    this.LblApplying.Text = "申请加入中...";
                    this.LblApplying.Visible = true;
                }
            }
            catch (Exception)
            {                
                Response.Write("<script>alert('操作失败')</script>");
            }
        }
        else if (this.linkBt_AplOrQt.Text == "退出组织")
        {
            try
            {
                GroupProvider.QuitGroup(orgid, uid);                
                
                {
                    this.linkBt_AplOrQt.Text = "申请加入";
                    this.linkBt_AplOrQt.Attributes.Clear();
                    this.LblApplying.Visible = true;

                }
            }
            catch (Exception)
            {
                Response.Write("<script>alert('操作失败')</script>");
            }
        }
    }
    protected void linkBt_InviteJoin_Click(object sender, EventArgs e)
    {
        
    }
    protected void LinkBt_EditOrg_Click(object sender, EventArgs e)
    {        
        Response.Redirect("GroupUpdate.aspx?orgid=" + orgid);
    }   
}
